<template>
	<button type="primary" @click="loadList" plain="true">登录</button>
</template>

<script>
	export default {
		data() {

		},
		created() {
			this.authLogin()
		},
		methods: {
			async loadList() {
				const result = await this.$request('/homeInfo')
				console.log(result)
			},
			async authLogin() {
				var _this = this;
				////弹窗授权
				//showModal用于触发uni.getUserProfile弹窗
				uni.showModal({
					title: '授权登录',
					content: '是否授权登录微信小程序？',
					success: () => {
						uni.getUserProfile({
							desc: '登录后可同步数据',
							lang: 'zh_CN',
							success: (infoRes) => {
								console.log('用户信息-----------', infoRes)
								//获取code
								uni.login({
									provider: 'weixin',
									success: (loginRes) => {
										console.log('获取code-----------', loginRes)
										//调用接口传递参数
										let parames = {
											code: loginRes.code,
											encryptedData: infoRes
												.encryptedData,
											iv: infoRes.iv,
											signature: infoRes.signature,
											rawData: infoRes.rawData
										};
										console.log('将参数传给后端-----------', parames)
										console.log(loginRes.code);
										uni.request({
											url: 'http://localhost/miniapp/api/authLogin',
											data: {
												code: loginRes.code,
												encryptedData: infoRes
													.encryptedData,
												iv: infoRes.iv,
											},
											method: 'GET'
										}).then((res) => {
											//获取到 openid 和 session_k后，自己的逻辑
											console.log("登录状态", res[1]
												.data);
											if (res[1].data.status ==
												1001) {
												console.log("登录状态1");
												//如果是app的登录
												_this.$api.msg(res[1]
													.message);
												uni.setStorageSync(_this
													.sessionKey, res[1]
													.data.token);
												_this.login(res[1].data);
												uni.navigateBack();
											} else {
												console.log("登录状态2");
												_this.$api.msg(res
													.message);
												_this.logining = false;
											}
										});
									},
									fail: function(err) {
										uni.showToast({
											icon: 'none',
											title: '授权失败'
										})
									}
								});
							},
							fail: function(err) {
								uni.showToast({
									icon: 'none',
									title: '授权失败'
								})
							}
						});
					}
				})
				//未弹窗授权
				// 获取用户信息， 传给后端换取openid / uuid
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						console.log('infoRes', infoRes)
						//获取code
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								console.log('loginRes', loginRes);
								//调用接口传递参数
								let parames = {
									code: loginRes.code,
									encryptedData: infoRes.encryptedData,
									iv: infoRes.iv,
									signature: infoRes.signature,
									rawData: infoRes.rawData
								};
							},
							fail: function(err) {
								uni.showToast({
									icon: 'none',
									title: '授权失败，请重新授权'
								})
							}
						});
					},
					fail: function(err) {
						uni.showToast({
							icon: 'none',
							title: '授权失败，请重新授权'
						})
					}
				});

			},
		}
	}
</script>

<style>
</style>
